// pages/reservation/reservation.js
const http = require("../../api/index")
const date = new Date()
const months = []
const days = []

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    year: date.getFullYear(),
    months,
    month: 1,
    days,
    day: 1,
    campId: "", //营地id
    userId: "", //用户id
    appointTime: '',
    time: '',
    CampCampsiteList: [], //营位
    projectList: [], //其他项目
    activityList: [], //活动列表,
    detailList: [
      // {
      //   campsiteId: '', //营位
      //   number: '',
      //   type: 1,
      // },
      // {
      //   projectId: '', //项目
      //   number: '',
      //   type: 3
      // },
      // {
      //   activityId: '', //活动
      //   number: '',
      //   type: 3
      // },
    ],
    amount: '',
    show: false,
  },
  hideTime() {
    this.setData({
      show: false
    })
  },
  confirm() {
    this.setData({
      time: this.data.year + '-' + this.data.month + '-' + this.data.day,
      show: false
    })
    console.log(this.data.time,"time")
  },
  showTime() {
    this.setData({
      show: true
    })
  },
  bindChange(e) {
    const val = e.detail.value
    this.setData({
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
    })
    console.log(this.data.years[val[0]],88);
  },
  // 绑定时间
  changeAppointTime(e) {
    this.setData({
      time: e.detail.value
    })
    console.log(e,4567);
  },
  minusC(e) {
    const index = e.currentTarget.dataset.index
    var Campsite = "CampCampsiteList[" + index + "].number";
    var unitPrice = "CampCampsiteList[" + index + "].unitPrice";
    let originalPrice = this.data.CampCampsiteList[index].originalPrice
    let number = this.data.CampCampsiteList[index].number
    if (number > 0) {
      this.setData({
        [Campsite]: number - 1,
        [unitPrice]: Number(originalPrice) * (number - 1)
      })
      this.getAmount()
    }
  },
  addC(e) {
    const index = e.currentTarget.dataset.index
    var Campsite = "CampCampsiteList[" + index + "].number";
    let number = this.data.CampCampsiteList[index].number
    var unitPrice = "CampCampsiteList[" + index + "].unitPrice";
    let originalPrice = this.data.CampCampsiteList[index].originalPrice
    this.setData({
      [Campsite]: number + 1,
      [unitPrice]: Number(originalPrice) * (number + 1)
    })
    this.getAmount()
  },
  minusA(e) {
    const index = e.currentTarget.dataset.index
    var activity = "activityList[" + index + "].number";
    let number = this.data.activityList[index].number
    var unitPrice = "activityList[" + index + "].unitPrice";
    let originalPrice = this.data.activityList[index].originalPrice
    if (number > 0) {
      this.setData({
        [activity]: number - 1,
        [unitPrice]: Number(originalPrice) * (number - 1)
      })
      this.getAmount()

    }
  },
  addA(e) {
    const index = e.currentTarget.dataset.index
    var activity = "activityList[" + index + "].number";
    let number = this.data.activityList[index].number
    var unitPrice = "activityList[" + index + "].unitPrice";
    let originalPrice = this.data.activityList[index].originalPrice
    this.setData({
      [activity]: number + 1,
      [unitPrice]: Number(originalPrice) * (number + 1)
    })
    this.getAmount()

  },
  minusO(e) {
    const index = e.currentTarget.dataset.index
    var otherProject = "projectList[" + index + "].number";
    let number = this.data.projectList[index].number
    var unitPrice = "projectList[" + index + "].unitPrice";
    let originalPrice = this.data.projectList[index].originalPrice
    if (number > 0) {
      this.setData({
        [otherProject]: number - 1,
        [unitPrice]: Number(originalPrice) * (number - 1)
      })
      this.getAmount()

    }
  },
  addO(e) {
    const index = e.currentTarget.dataset.index
    var otherProject = "projectList[" + index + "].number";
    let number = this.data.projectList[index].number
    var unitPrice = "projectList[" + index + "].unitPrice";
    let originalPrice = this.data.projectList[index].originalPrice
    this.setData({
      [otherProject]: number + 1,
      [unitPrice]: Number(originalPrice) * (number + 1)
    })
    this.getAmount()

  },
  // 营位
  getCampCampsite(campId) {
    http.getCampCampsite(campId).then((res) => {
      // console.log(res, '123123');
      if (res.code === 200 && res.data.length !== 0) {
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].factor !== null) {
            res.data[i].factors = []
            const list = res.data[i].factor.split(',')
            for (let j = 0; j < list.length; j++) {
              if (list[j] == 1) {
                res.data[i].factors.push("有水")
              } else if (list[j] == 2) {
                res.data[i].factors.push("有电")
              } else if (list[j] == 3) {
                res.data[i].factors.push("无水")
              } else if (list[j] == 4) {
                res.data[i].factors.push("无水")
              } else if (list[j] == 5) {
                res.data[i].factors.push("有新能源车充电桩")
              } else if (list[j] == 6) {
                res.data[i].factors.push("有卫生间")
              } else {
                res.data[i].factors.push("其他条件")
              }
            }
          }
        }
        let list = res.data.map(item => {
          return {
            ...item,
            number: 0,
            type: 1,
            unitPrice: 0
          }
        })
        this.setData({
          CampCampsiteList: list
        })
      }
    })
  },
  // 其他项目
  getProject(campId) {
    http.getProject(campId).then((res) => {
      // console.log(res, '456456');
      if (res.code === 200 && res.data) {
        let list = res.data.map(item => {
          return {
            ...item,
            number: 0,
            type: 2,
            unitPrice: 0
          }
        })
        this.setData({
          projectList: list
        })
      }
    })
  },
  // 活动列表
  getActivity(campId) {
    http.getActivity(campId).then((res) => {
      // console.log(res, '123123');
      if (res.code === 200 && res.data) {
        let list = res.data.map(item => {
          return {
            ...item,
            number: 0,
            type: 3,
            unitPrice: 0
          }
        })
        this.setData({
          activityList: list
        })
      }
    })
  },
  bindChange(e) {
    const val = e.detail.value
    this.setData({
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      isDaytime: !val[3]
    })
  },
  // 总金额
  getAmount() {
    const c = this.data.CampCampsiteList.map(item => {
      let cPrice = Number(item.originalPrice) * item.number
      return cPrice
    })
    let cAll = 0
    for (let i = 0; i < c.length; i++) {
      cAll = cAll + c[i]
    }
    const o = this.data.projectList.map(item => {
      let oPrice = Number(item.originalPrice) * item.number
      return oPrice
    })
    let oAll = 0
    for (let i = 0; i < o.length; i++) {
      oAll = oAll + o[i]
    }
    const a = this.data.activityList.map(item => {
      let aPrice = Number(item.originalPrice) * item.number
      return aPrice
    })
    let aAll = 0
    for (let i = 0; i < a.length; i++) {
      aAll = aAll + a[i]
    }
    this.setData({
      amount: cAll + oAll + aAll
    })
  },
  // 预约
  toReservation() {
    if (this.data.time !== '') {
      let replacedStr = this.data.time;
      let parsedDate = new Date(replacedStr);
      this.setData({
        detailList: [...this.data.CampCampsiteList, ...this.data.projectList, ...this.data.activityList],
        appointTime: parsedDate
      })
      http.addAppointment({
        "amount": this.data.amount,
        "appointTime": this.data.time,
        "campId": this.data.campId,
        "detailsList": this.data.detailList,
        "userId": this.data.userId
      }).then((res) => {
        console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '预约成功！',
            icon: 'success',
            duration: 1500
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          wx.showToast({
            title: '预约失败！',
            icon: 'error',
            duration: 1500
          })
        }
      })
    }else{
      wx.showToast({
        title: '请选择预约时间',
        icon:"error",
        duration:1500
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const campId = options.campId
    this.setData({
      campId: campId,
      userId: wx.getStorageSync('userInfo').userId
    })
    this.getCampCampsite(campId)
    this.getProject(campId)
    this.getActivity(campId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})